package com.cos.shop.common;

import java.util.List;

/**
 * Copyright (C),2021-2022,亚瑟王
 * FileName: PageHelper
 * Author: saber
 * Date:2022/9/17 11:02
 */
public class PageHelper<T> {
    //当前页码
    private int page;
    //每页显示行数
    private int pageSize;
    //数据总条数
    private int totalCount;
    //总页数
    private int totalPage;
    //起始行下标
    private int startRow;
    // 分页的列表数据
    private List<T> list;

    @Override
    public String toString() {
        return "PageHelper{" +
                "page=" + page +
                ", pageSize=" + pageSize +
                ", totalCount=" + totalCount +
                ", totalPage=" + totalPage +
                ", startRow=" + startRow +
                ", list=" + list +
                '}';
    }

    public PageHelper(int page, int pageSize, int totalCount) {
        //1 接受数据总条数
        this.totalCount = totalCount;
        //2 接受并显示pageSize 每夜显示行数
        if (pageSize < 1) {
            //数值是小于1,设置默认为8
            this.pageSize = 8;
        } else {
            this.pageSize = pageSize;
        }
        //3 计算总页数
        this.totalPage=(int)Math.ceil(this.totalCount/(this.pageSize+0.0));
        //4 接受并限制当前页码
        if (page<1){
            //当前页码小于1,设置默认为1
            this.page=1;
        }else if(page>this.totalPage){
            //当前页码大于总页数,设置为最后一页的值
            this.page=this.totalPage;
        }else{
            //合法值 直接赋值
            this.page=page;
        }
        //5 计算 起始行下标
        this.startRow=(this.page-1)*this.getPageSize();
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getStartRow() {
        return startRow;
    }

    public void setStartRow(int startRow) {
        this.startRow = startRow;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }
} 
